<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="流水号名称" prop="moduleName">
        <el-input
          v-model="queryParams.moduleName"
          placeholder="请输入流水号名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="流水号代码" prop="moduleCode">
        <el-input
          v-model="queryParams.moduleCode"
          placeholder="请输入流水号代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="码头" prop="companyCod">

        <el-select v-model="queryParams.companyCod" clearable placeholder="请选择码头">
          <el-option
            v-for="item in companyCodeOptions"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:serial:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:serial:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:serial:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:serial:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serialList" @selection-change="handleSelectionChange" fit border width="100%">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="ID" align="center" prop="id" fixed/>
      <el-table-column label="流水号名称" align="center" prop="moduleName" width="100" show-overflow-tooltip fixed/>
      <el-table-column label="当前值" align="center" prop="currentNum" />
      <el-table-column label="流水号代码" align="center" prop="moduleCode" width="180" show-overflow-tooltip/>
      <el-table-column label="流水号前缀" align="center" prop="preWord" width="100" show-overflow-tooltip/>
      <el-table-column label="时间格式" align="center" prop="timeFormat" width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.time_format" :value="scope.row.timeFormat"/>
        </template>
      </el-table-column>
      <el-table-column label="后缀位数" align="center" prop="sufNum" />
      <el-table-column label="后缀代码" align="center" prop="sufWord" />
      <el-table-column label="是否每日清空" align="center" prop="isDaily" width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_daily" :value="scope.row.isDaily"/>
        </template>
      </el-table-column>
      <el-table-column label="是否月度清空" align="center" prop="isMonth" width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_month" :value="scope.row.isMonth"/>
        </template>
      </el-table-column>
      <el-table-column label="是否年度清空" align="center" prop="isYear" width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_year" :value="scope.row.isYear"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="部门" align="center" prop="deptName" width="100"/>-->
      <el-table-column label="码头" prop="companyCod" align="center" :formatter="companyCodeFormat" />
      <el-table-column label="合同年份" align="center" prop="contractYear" />
      <el-table-column label="合同类型" align="center" prop="contractSpType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.contract_sp_type" :value="scope.row.contractSpType"/>
        </template>
      </el-table-column>

      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip/>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" show-overflow-tooltip/>
<!--      <el-table-column label="乐观锁" align="center" prop="version" />-->
      <el-table-column label="操作" align="center"  width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:serial:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:serial:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流水号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-row>

          <el-col :span="12">
            <el-form-item label="流水号名称" prop="moduleName">
              <el-input v-model="form.moduleName" placeholder="请输入流水号名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="流水号代码" prop="moduleCode">
              <el-input v-model="form.moduleCode" placeholder="请输入流水号代码" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="流水号前缀" prop="preWord">
              <el-input v-model="form.preWord" placeholder="请输入流水号前缀" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="时间格式" prop="timeFormat">
              <el-select v-model="form.timeFormat" placeholder="请选择时间格式">
                <el-option
                  v-for="dict in dict.type.time_format"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>



          <el-col :span="12">
            <el-form-item label="后缀位数" prop="sufNum">
              <el-input v-model="form.sufNum" placeholder="请输入后缀位数" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="后缀代码" prop="sufWord">
              <el-input v-model="form.sufWord" placeholder="请输入后缀代码" />
            </el-form-item>
          </el-col>



          <el-col :span="12">
            <el-form-item label="是否每日清空" prop="isDaily">
              <el-select v-model="form.isDaily" placeholder="请选择是否每日清空">
                <el-option
                  v-for="dict in dict.type.is_daily"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否月度清空" prop="isMonth">
              <el-select v-model="form.isMonth" placeholder="请选择是否月度清空">
                <el-option
                  v-for="dict in dict.type.is_month"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="12">
            <el-form-item label="是否年度清空" prop="isYear">
              <el-select v-model="form.isYear" placeholder="请选择是否年度清空">
                <el-option
                  v-for="dict in dict.type.is_year"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="12">
            <el-form-item label="码头" prop="companyCod">
              <el-select v-model="form.companyCod" clearable placeholder="请选择码头" >
                <el-option
                  v-for="item in companyCodeOptions"
                  :key="item.key"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="合同年份" prop="contractYear">
              <el-input v-model="form.contractYear" placeholder="请输入合同年份" clearable/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="合同类型" prop="contractSpType">
              <el-select v-model="form.contractSpType" placeholder="请选择合同类型" clearable filterable>
                <el-option
                  v-for="dict in dict.type.contract_sp_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listSerial, getSerial, delSerial, addSerial, updateSerial } from "@/api/base/serial";
import { deptTreeSelect } from '@/api/system/user'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getCompanyCodeOptions} from "@/api/options/options";
export default {
  name: "Serial",
  dicts: ['time_format', 'is_year', 'is_daily', 'is_month', 'contract_sp_type'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流水号表格数据
      serialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        moduleName: null,
        moduleCode: null,
        preWord: null,
        sufWord: null,
        isDaily: null,
        isMonth: null,
        isYear: null,
        deptId: null,
        companyCod:null,
        contractYear: null,
        contractSpType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        moduleName: [
          { required: true, message: "流水号名称不能为空", trigger: "blur" }
        ],
        moduleCode: [
          { required: true, message: "流水号代码不能为空", trigger: "blur" }
        ],
        preWord: [
          { required: true, message: "流水号前缀不能为空", trigger: "blur" }
        ],
        timeFormat: [
          { required: true, message: "时间格式不能为空", trigger: "change" }
        ],
        sufNum: [
          { required: true, message: "后缀位数不能为空", trigger: "blur" }
        ],
      },
      deptOptions: undefined,
      companyCodeOptions:[],
    };
  },
  created() {
    this.getDeptTree()
    this.getCompanyCodeLabel()
    this.getList();
  },
  methods: {
    getCompanyCodeLabel() {
      getCompanyCodeOptions().then(res => {
        this.companyCodeOptions = res.data;
      });
    },

    companyCodeFormat(row,column){

      return this.selectDictLabel(this.companyCodeOptions,row.companyCod)

    },
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询流水号列表 */
    getList() {
      this.loading = true;
      listSerial(this.queryParams).then(response => {
        this.serialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        moduleName: null,
        currentNum: null,
        moduleCode: null,
        preWord: null,
        timeFormat: null,
        sufNum: null,
        sufWord: null,
        isDaily: null,
        isMonth: null,
        isYear: null,
        deptId: null,
        companyCod:null,
        contractYear: null,
        contractSpType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加流水号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSerial(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流水号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSerial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSerial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除流水号编号为"' + ids + '"的数据项？').then(function() {
        return delSerial(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/serial/export', {
        ...this.queryParams
      }, `serial_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
